﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Pixer
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }


        private void canvas_MouseDown(object sender, MouseEventArgs e)
        {
            canvas.StartPoint = e.Location;
            canvas.CurrentPoint = e.Location;
            canvas.MouseState = 1;
            canvas.MouseStateChanged = true;
            canvas.Refresh();
        }

        private void canvas_MouseMove(object sender, MouseEventArgs e)
        {
            canvas.CurrentPoint = e.Location;
            canvas.Refresh();
        }

        private void canvas_MouseUp(object sender, MouseEventArgs e)
        {
            canvas.EndPoint = e.Location;
            canvas.MouseState = 0;
            canvas.MouseStateChanged = true;
            canvas.Refresh();
        }

        private void canvas_DoubleClick(object sender, EventArgs e)
        {
            canvas.MouseState = 2;
            canvas.MouseStateChanged = true;
            canvas.Refresh();
        }
        /// <summary>
        /// 画点模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiPoint_Click(object sender, EventArgs e)
        {
            canvas.Mode = Mode.PIXEL;
        }

        private void tsmiLine_Click(object sender, EventArgs e)
        {
            canvas.Mode = Mode.LINE;
        }


        private void tsmiLzsb_Click(object sender, EventArgs e)
        {
            canvas.Mode = Mode.LZSB;
        }

        private void tsmiCircle_Click(object sender, EventArgs e)
        {
            canvas.Mode = Mode.CIRCLE;
        }

        private void tsmiEllipse_Click(object sender, EventArgs e)
        {
            canvas.Mode = Mode.ELLIPSE;
        }



        private void MainForm_Load(object sender, EventArgs e)
        {
            Bitmap pic = new Bitmap(canvas.Width, canvas.Height);
            canvas.Image = pic;
            tssbLineColor.Image = getColorBitmap(canvas.LineColor);
            tssbFillColor.Image = getColorBitmap(canvas.FillColor);
             //初始化边框
            Rectangle rect = new Rectangle();
            rect.Position = new Point(0, 0);
            rect.Width = pic.Width-1;
            rect.Height = pic.Height-1;
            rect.LineColor = Color.Black;
            rect.paint(pic);
            Fill f = new Fill();
            f.Position = new Point(1, 1);
            f.FillColor = Color.White;
            f.paint(pic);
        }

        private Bitmap getColorBitmap(Color c)
        {
            Bitmap tmp = new Bitmap(1, 1);
            tmp.SetPixel(0, 0, c);
            return tmp;
        }

        private void tsmiCurve_Click(object sender, EventArgs e)
        {
            canvas.Mode = Mode.CURVE;
        }

        private void tsmiRect_Click(object sender, EventArgs e)
        {
            canvas.Mode = Mode.RECTANGLE;
        }

        private void tsmiFill_Click(object sender, EventArgs e)
        {
            canvas.Mode = Mode.FILL;
        }
        /// <summary>
        /// 设置线条颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tssbLineColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            canvas.LineColor = colorDialog1.Color;
            tssbLineColor.Image = getColorBitmap(canvas.LineColor);
        }
        /// <summary>
        /// 设置填充颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tssbFillColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            canvas.FillColor = colorDialog1.Color;
            tssbFillColor.Image = getColorBitmap(canvas.FillColor);
        }

        private void tsmiFSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "bitmap|*.bmp";
            sfd.ShowDialog();
            try
            {
                String path = sfd.FileName;

                canvas.Image.Save(path);
            }
            catch { }
        }

       
    }
}
